<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="baidu-site-verification" content="SCzNOI2Wq6" />
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>
        daozhuang
    </title>
    
<link rel="stylesheet" href="/libs/highlight/styles/monokai-sublime.css">

    
<link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">

    
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body id="bodyx">
    <div class="hd posts">
    <a href="/index.html"><i class="fa fa-reply replay-btn" aria-hidden="true"></i></a>
    <div class="post-title">
        <p>
            Mybatis之mapper xml遇到的问题
        </p>
        <hr>
    </div>
    <div class="post-content">
        <h2 id="trim-标签的使用-mapper-xml去除结尾逗号"><a href="#trim-标签的使用-mapper-xml去除结尾逗号" class="headerlink" title="trim 标签的使用 mapper.xml去除结尾逗号"></a>trim 标签的使用 mapper.xml去除结尾逗号</h2><p> trim 标签常用于去掉结尾的 逗号<br>                prefix：前缀覆盖并增加其内容<br>                suffix：后缀覆盖并增加其内容<br>                prefixOverrides：前缀判断的条件<br>                suffixOverrides：后缀判断的条件</p>
<h2 id="if-标签忽略为空的字段"><a href="#if-标签忽略为空的字段" class="headerlink" title="if 标签忽略为空的字段"></a>if 标签忽略为空的字段</h2><p>通常我们在dao层即mapper.xml中使用 if标签 进行过滤判断操作，如下</p>
<p>trim 标签 和 if 标签 使用方法如下代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;insert id&#x3D;&quot;inserttable&quot; parameterType&#x3D;&quot;map&quot;&gt;</span><br><span class="line">	INSERT INTO tablename</span><br><span class="line">	&lt;trim prefix&#x3D;&quot;(&quot; suffix&#x3D;&quot;)&quot; suffixOverrides&#x3D;&quot;,&quot; &gt;</span><br><span class="line">		&lt;if test&#x3D;&quot;value!&#x3D;&#39;&#39; and value !&#x3D; null&quot;&gt;</span><br><span class="line">		value,</span><br><span class="line">		&lt;&#x2F;if&gt;</span><br><span class="line">	&lt;&#x2F;trim&gt;</span><br><span class="line">	&lt;trim prefix&#x3D;&quot;values(&quot; suffix&#x3D;&quot;)&quot; suffixOverrides&#x3D;&quot;,&quot; &gt;</span><br><span class="line">		&lt;if test&#x3D;&quot;value!&#x3D;&#39;&#39; and value !&#x3D; null&quot;&gt;</span><br><span class="line">			#&#123;value&#125;,</span><br><span class="line">		&lt;&#x2F;if&gt;</span><br><span class="line">	&lt;&#x2F;trim&gt;</span><br><span class="line">&lt;&#x2F;insert&gt;</span><br></pre></td></tr></table></figure>
<h2 id="插入一条记录时返回该条记录的自增主键值"><a href="#插入一条记录时返回该条记录的自增主键值" class="headerlink" title="插入一条记录时返回该条记录的自增主键值"></a>插入一条记录时返回该条记录的自增主键值</h2><p>使用MyBatis往MySQL数据库中插入一条记录后，返回该条记录的自增主键值。<br>Mybatis的Mapper的标签中有一个属性<br>Mybatis 配置文件 useGeneratedKeys 参数只针对 insert 语句生效，默认为 false。<br>当设置为 true 时，表示如果插入的表以自增列为主键，则允许 JDBC 支持自动生成主键，并可将自动生成的主键返回。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- useGeneratedKeys默认值为false，keyProperty的值对应的是User类中的主键名 --&gt;</span><br><span class="line">&lt;insert id&#x3D;&quot;&quot; parameterType&#x3D;&quot;&quot; useGeneratedKeys&#x3D;&quot;true&quot; keyProperty&#x3D;&quot;id&quot;&gt;</span><br><span class="line">	insert into user(name, password) values(#&#123;name&#125;, #&#123;password&#125;)	</span><br><span class="line">&lt;&#x2F;insert&gt;</span><br></pre></td></tr></table></figure>
<h2 id="mybatis中使用mysql的模糊查询字符串拼接（like）"><a href="#mybatis中使用mysql的模糊查询字符串拼接（like）" class="headerlink" title="mybatis中使用mysql的模糊查询字符串拼接（like）"></a>mybatis中使用mysql的模糊查询字符串拼接（like）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id&#x3D;&quot;&quot; resultType&#x3D;&quot;&quot;&gt;</span><br><span class="line">    SELECT * FROM table where name like concat(&#39;%&#39;,#&#123;name&#125;,&#39;%&#39;)</span><br><span class="line">&lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>
<h2 id="mybatis-判断参数要加-toString"><a href="#mybatis-判断参数要加-toString" class="headerlink" title="mybatis 判断参数要加.toString()"></a>mybatis 判断参数要加.toString()</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;if test&#x3D;&quot;params.selectTime &#x3D;&#x3D;&#39;2&#39;.toString() &quot;&gt;</span><br></pre></td></tr></table></figure>


    </div>

    
        <hr class="fhr">
        <div id="vcomments"></div>
    
</div>
    <div class="footer" id="footer">
    <p>Copyright © 2020 <a class="flink" href="https://hexo.io" target="_blank" rel="noopener">Hexo</a>-<a class="flink" href="https://github.com/sanjinhub/hexo-theme-geek" target="_blank" rel="noopener">Geek</a>.
        <label class="el-switch el-switch-green el-switch-sm" style="vertical-align: sub;">
            <input type="checkbox" name="switch" id="update_style">
            <span class="el-switch-style"></span>
        </label>
<!--         <script type="text/javascript">
        var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
        document.write(unescape("%3Cspan id='cnzz_stat_icon_1278548644'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/stat.php%3Fid%3D1278548644%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
        </script> -->
    </p>
</div>
<input type="hidden" id="web_style" value="black">
<input type="hidden" id="valine_appid" value="c994h5wXLa8fSMa424BTyVhs-gzGzoHsz">
<input type="hidden" id="valine_appKey" value="4tFbwCi4sYo3yRFgiNFpQd4N">

<script src="/libs/jquery.min.js"></script>


<script src="/libs/highlight/highlight.pack.js"></script>

<script src='//cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>

<script src="/js/js.js"></script>

<style type="text/css">
.v * {
    color: #698fca;
}

.v .vlist .vcard .vhead .vsys {
    color: #3a3e4a;
}

.v .vlist .vcard .vh .vmeta .vat {
    color: #638fd5;
}

.v .vlist .vcard .vhead .vnick {
    color: #6ba1ff;
}

.v a {
    color: #8696b1;
}

.v .vlist .vcard .vhead .vnick:hover {
    color: #669bfc;
}
</style>
</body>

</html>